Canvas Widget

Path: Widget Gallery> Basic> Generic Canvas

Canvas widget can be used to draw graphic via JavaScript scripting.

Note: the JavaScript methods are the same that are available for the HTML5 <canvas> tag

Parameter Description

Canvas Width
Canvas Height

Canvas size.

Note this is not the widget size. For example, the canvas size could be 500x500 pixels where the widget size could be 100x100 pixels. Draw Hint parameter will define how to stretch the canvas size to fit the widget size.

Draw Hint

Define how fit the canvas inside the widget size

  • Clip
    No Transformation is applied, coordinate system is not scaled and drawing is clipped inside the widget bounding rectangle.
  • Fit to size
    Fit to the widget size preserving the canvas model aspect ratio.
  • Stretch
    Fit to the widget size ignoring the canvas model aspect ratio.

Example using a Canvas size larger than the widget size:

Design Time Preview Canvas preview inside JMobile Studio

Note the JavaScript code could use data not available inside JMobile Studio but only inside the HMI device

Auto Clear Background Automatic clear the background before draw canvas. When disabled, the painted items are persisted and is not necessary redraw everything from scratch.
OnDraw Action

The OnDraw event is executed when the page is painted. This event has to be linked with the JavaScript code that draws the canvas graphic.

OnMousePress Action
OnMouseRelease Actions
OnMouseDrag Actions

Mouse events
Available Canvas Methods

// Painter Save/Restore

// Scale/Transform

// Gradient

// Rectangle Functions

// Path

// Drawing Text

// Arc

// Fill/Stroke

// Image manipulation (Draw CImageWgt using target and source rect)

// Pixel manipulation

 

Canvas JavaScript Example

The canvas is initially blank. To display something, a script first needs to access the rendering context and draw on it:

var ctx = me.context2d;

then you can use the canvas methods, as in the below example

function GenericCanvasWgt1_onDraw(me, eventInfo)
{
    var ctx = me.context2d;
    ctx.fillStyle = 'red';
    ctx.fillRect(0,0,250,250);
    ctx.fillStyle = 'green';
    ctx.fillRect(250,0,250,250);
    ctx.fillStyle = 'blue';
    ctx.fillRect(0,250,250,250);
    ctx.fillStyle = 'black';
    ctx.fillRect(250,250,250,250);
}
				
function GenericCanvasWgt1_onMouseDown(me, eventInfo)
{
    alert("X = " + eventInfo.posX + "\nY = " + eventInfo.posY );
}			

The update method can be used to dynamically redraw a canvas widget

function BtnStd1_btn_onMouseClick(me, eventInfo)
{
    var myCanvasWidget = page.getWidget("GenericCanvasWgt1");    
    myCanvasWidget.update()
}